package com.ruangong.GitClient.app;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.content.Intent;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

import com.ruangong.GitClient.R;
import com.ruangong.GitClient.HunHePanAPI.*;

public class TabSource extends AppCompatActivity {

    private ListView searchResultListView;
    private ArrayAdapter<MovieLink> adapter;
    private List<MovieLink> searchResults;
    private Tab mytab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_source);

        searchResultListView = findViewById(R.id.websiteListView);
        mytab = (Tab) getIntent().getSerializableExtra("TAB_MASSAGE");
        //文本设置

        TextView titleTextView = findViewById(R.id.titleTextView);
        titleTextView.setText(mytab.getName());
        // 初始化搜索结果列表
        searchResults = new ArrayList<>();
        adapter = new MovieLinkAdapter(this, searchResults);

        searchResultListView.setAdapter(adapter);

        // 执行搜索操作
        performGetTabSource();
        // 设置列表项的点击事件
        searchResultListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取点击的项的内容
                MovieLink clickedMovieLink = searchResults.get(position);
                // 创建跳转到另一个Activity的意图
                Intent intent = new Intent(TabSource.this, ResourceDetails.class);
                // 在Intent中传递额外的数据，如果需要的话
                // intent.putExtra("key", value);
                // 启动另一个Activity
                intent.putExtra("MOVIE_LINK", clickedMovieLink);
                startActivity(intent);
            }
        });
    }

    // 执行搜索操作
    private void performGetTabSource() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                // 在这里执行网络请求

                List<MovieLink> movieLinks = getTabID.getResult(mytab.getID());

                searchResults.clear();
                if (movieLinks == null || movieLinks.isEmpty()) {
                    searchResults.add(new MovieLink("", "暂无", "","")); // 添加一个标记项
                    searchResultListView.setOnItemClickListener(null);
                } else {
                    // 将搜索结果添加到 searchResults 列表中
                    for (MovieLink link : movieLinks) {
                        searchResults.add(link);
                    }
                }
                // 切换回主线程更新UI
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 更新UI
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();




    }
}